package cn.stylefeng.guns.modular.gridsystem.api.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.*;
import cn.stylefeng.guns.modular.gridsystem.model.param.*;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizDeviceResult;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizExceptionLogResult;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizGatewayResult;
import cn.stylefeng.guns.modular.gridsystem.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * 第三方web页面接口
 */
@RestController
@RequestMapping("/web/api")
@Slf4j
public class ApiWebController {

    @Autowired
    private BizGatewayService bizGatewayService;

    @Autowired
    private BizExceptionService bizExceptionService;

    @Autowired
    private BizExceptionLogService exceptionLogService;

    @Autowired
    private BizIndustryInvolvedService industryInvolvedService;

    @Autowired
    private  BizFaultModelService faultModelService;

    @Autowired
    private BizStationService stationService;

    @Autowired
    private BizDeviceService deviceService;

    @GetMapping("/getIndustryInvolved")
//    @ApiOperation("查询行业列表")
    public ResponseData getIndustryInvolved() {
        List<BizIndustryInvolved> list = industryInvolvedService.list();
        return ResponseData.success(list);
    }

    @PostMapping("/getFaultModelList")
    public ResponseData getFaultModelList(@RequestParam(name = "mId",required = false)Integer mId,Integer groupId) {
        BizFaultModelParam param = new BizFaultModelParam();
        if(ObjectUtil.isNotEmpty(mId)){
            param.setId(mId);
        }
        if(ObjectUtil.isNotNull(groupId)){
            param.setGroupId(groupId);
        }
        List<BizFaultModel> list = faultModelService.list(param);
        return ResponseData.success(list);
    }

    @PostMapping("/getStationInfo")
    public ResponseData getStationInfo(@RequestParam(name = "stationId") Integer stationId) {
        BizStation station = stationService.getById(stationId);
        return ResponseData.success(station);
    }

    @PostMapping("/getDeviceList")
//    @ApiOperation("查询设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "stationId", dataType = "int", required = true, value = "电站Id"),
            @ApiImplicitParam(paramType = "query", name = "industryInvolvedId", dataType = "int",value = "行业id"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", dataType = "int", required = true, value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", required = true, value = "每页条数")
    })
    public ResponseData getDeviceList( @RequestParam(name = "stationId")int stationId,
                                       @RequestParam(name = "industryInvolvedId")Integer industryInvolvedId,
                                       @RequestParam(name = "pageNo") int pageNo,
                                       @RequestParam(name = "pageSize") int pageSize) {
        BizDeviceParam deviceParam = new BizDeviceParam();
        deviceParam.setStationId(stationId);
        if(ObjectUtil.isNotEmpty(industryInvolvedId)){
            deviceParam.setIndustryId(industryInvolvedId);
        }
        PageResult<BizDeviceResult> page = deviceService.page(deviceParam);
        return ResponseData.success(page);
    }

    @PostMapping("/getAllDeviceList")
    public ResponseData getAllDeviceList(@RequestParam(name = "deviceId",required = false) Integer deviceId) {
        BizDeviceParam deviceParam  = new BizDeviceParam();
        if(ObjectUtil.isNotEmpty(deviceId)){
            deviceParam.setId(deviceId);
        }
        return ResponseData.success(deviceService.list(deviceParam));
    }

    @PostMapping("/getGatewayChannel")
//    @ApiOperation("查询设备对应的网关与网关通道")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "int", required = true, value = "设备Id")
    })
    public ResponseData getGatewayChannel(@RequestParam(name = "deviceId")int deviceId) {
        BizDevice device = deviceService.getById(deviceId);
        Integer stationId = device.getStationId();
        LambdaQueryWrapper<BizGateway> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizGateway::getStationId,stationId);
        List<BizGatewayResult> list = bizGatewayService.getListAndChannel(queryWrapper);
        return ResponseData.success(list);
    }

    @PostMapping("/getExceptionLog")
//    @ApiOperation("查询故障列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", dataType = "String", required = true, value = "设备Id"),
            @ApiImplicitParam(paramType = "query", name = "startTime", dataType = "String", value = "开始日期"),
            @ApiImplicitParam(paramType = "query", name = "endTime", dataType = "String", value = "结束日期"),
            @ApiImplicitParam(paramType = "query", name = "micRouter", dataType = "String", value = "通道号"),
            @ApiImplicitParam(paramType = "query", name = "gatewayCode", dataType = "String",value = "网关编号"),
            @ApiImplicitParam(paramType = "query", name = "accessToken", dataType = "String", value = "模型编号"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", dataType = "int", required = true, value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", required = true, value = "每页条数")
    })
    public ResponseData getExceptionLog(@RequestParam(name="deviceId")int deviceId,
                                        @RequestParam(name="startTime",required = false) String startTime,
                                        @RequestParam(name="endTime",required = false) String endTime,
                                        @RequestParam(name = "micRouter",required = false) String micRouter,
                                        @RequestParam(name="gatewayCode",required = false) String gatewayCode,
                                        @RequestParam(name="accessToken",required = false) String accessToken,
                                        @RequestParam(name = "pageNo") int pageNo,
                                        @RequestParam(name = "pageSize") int pageSize) {
        QueryWrapper<BizException> wrapper = new QueryWrapper<>();
        wrapper.eq("device_id",deviceId);
        if(ObjectUtil.isNotEmpty(startTime)){
            wrapper.ge("created_at",DateUtil.parse(startTime));
        }
        if(ObjectUtil.isNotEmpty(endTime)){
            wrapper.le("created_at",DateUtil.parse(endTime));
        }
        if(ObjectUtil.isNotEmpty(gatewayCode)){
            wrapper.eq("gateway_code",gatewayCode);
        }
        if(ObjectUtil.isNotEmpty(accessToken)){
            wrapper.eq("access_token",accessToken);
        }
        List<BizException> bizExceptions = bizExceptionService.list(wrapper);
        Set<Integer> set = new HashSet<>();
        for (BizException bizException : bizExceptions) {
            Integer id = bizException.getId();
            set.add(id);
        }
        BizExceptionLogParam exceptionLogParam = new BizExceptionLogParam();
        if(ObjectUtil.isNotEmpty(micRouter)){
            exceptionLogParam.setGatewayChannel(micRouter);
        }
        PageResult<BizExceptionLogResult> page = exceptionLogService.thirdPageList(exceptionLogParam,set,deviceId);
        return ResponseData.success(page);
    }

    @PostMapping("/getExceptionLogInfo")
//    @ApiOperation("查询故障详情")
    public ResponseData getExceptionLogInfo(@RequestParam(name="eId")Integer eId) {
        BizExceptionLogResult exceptionLog = exceptionLogService.getExceptionLog(eId);
        return ResponseData.success(exceptionLog);
    }

}
